package com.example.service;

import com.example.bean.Collect;
import com.example.bean.Goods;
import com.example.bean.Result;
import com.example.mapper.CollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectService {
    @Autowired
    CollectMapper collectMapper;

    //查询商家单个商品收藏的次数
    public int queryNumber(int gId) {
        int collectNumber = collectMapper.collectNumber(gId);
        return collectNumber;
    }

    //查询用户收藏列表
    public List<Goods> SelectGoodsByUser(int uId) {
        List<Goods> goods = collectMapper.SelectGoodsByUser(uId);
        return goods;
    }

    //根据用户id和商品id插入收藏信息
    public Result insertById(int uid, int gid) {
        int i = collectMapper.ins(uid, gid);
        if (i == 1) {
            return new Result(200, "收藏成功", "");
        } else {
            return new Result(400, "收藏失败", "");
        }
    }

    //判断用户是否收藏某商品
    public Result selectById(int uid, int gid) {
        //判断用户是否收藏过该商品
        Collect collect = collectMapper.selectById(uid, gid);
        if (collect != null) {    //用户收藏过
            return new Result(200, "已经收藏过了", "");
        } else {
            return new Result(400, "用户未收藏", "");
        }
    }

    //根据用户id和商品id删除收藏信息
    public Result deleteCollect(int uid, int gid) {
        int i = collectMapper.deleteCollect(uid, gid);
        if (i != 0) {
            return new Result(200, "取消收藏成功", "");
        } else {
            return new Result(400, "取消收藏失败", "");
        }
    }
}
